'use strict';

const { Service } = require('egg');
const ModelName = 'Dict'
class RouterService extends Service {

    async refresh({ key }) {
        // 查询出来所有的字典，将其他表中的字段中文字段对应刷新
        const dictList = await this.ctx.model[ModelName].findAll();
        // 转换map
        const dictMap = {};
        dictList.forEach(item => {
            dictMap[item.id] = item.name;
        });
        // 更新autoCollect表
        for (const dictMapKey in dictMap) {
            /**
             * autocollect
             */
            if (key === 'autocollect') {
                const autocollectList = await this.ctx.model.RabbitAutoCollect.findAll({});
                const roomList = []
                const roomOutList = []
                const useList = []
                const monitorKeyList = []
                autocollectList.forEach(item => {
                    //  去重复
                    if (roomList.indexOf(item.room) === -1) {
                        roomList.push(item.room)
                    }
                    if (roomOutList.indexOf(item.room_out) === -1) {
                        roomOutList.push(item.room_out)
                    }
                    if (useList.indexOf(item.use) === -1) {
                        useList.push(item.use)
                    }
                    if (monitorKeyList.indexOf(item.monitor_key) === -1) {
                        monitorKeyList.push(item.monitor_key)
                    }
                });
                // 更新room
                for (let i = 0; i < roomList.length; i++) {
                    await this.ctx.model.RabbitAutoCollect.update({
                        room_text: dictMap[roomList[i]],
                    }, {
                        where: {
                            room: roomList[i],
                        }
                    })
                }
                // 更新room_out
                for (let i = 0; i < roomOutList.length; i++) {
                    await this.ctx.model.RabbitAutoCollect.update({
                        room_out_text: dictMap[roomOutList[i]],
                    }, {
                        where: {
                            room_out: roomOutList[i],
                        }
                    })
                }
                // 更新use
                for (let i = 0; i < useList.length; i++) {
                    await this.ctx.model.RabbitAutoCollect.update({
                        use_text: dictMap[useList[i]],
                    }, {
                        where: {
                            use: useList[i],
                        }
                    })
                }
                // 更新monitor_key
                for (let i = 0; i < monitorKeyList.length; i++) {
                    await this.ctx.model.RabbitAutoCollect.update({
                        monitor_key_text: dictMap[monitorKeyList[i]],
                    }, {
                        where: {
                            monitor_key: monitorKeyList[i],
                        }
                    })
                }
            }

            /**
             * collect
             */
            if (key === 'collect') {
                const autocollectList = await this.ctx.model.RabbitCollect.findAll({});
                const roomList = []
                const roomOutList = []
                const useList = []
                const monitorKeyList = []
                autocollectList.forEach(item => {
                    //  去重复
                    if (roomList.indexOf(item.room) === -1) {
                        roomList.push(item.room)
                    }
                    if (roomOutList.indexOf(item.room_out) === -1) {
                        roomOutList.push(item.room_out)
                    }
                    if (useList.indexOf(item.use) === -1) {
                        useList.push(item.use)
                    }
                    if (monitorKeyList.indexOf(item.monitor_key) === -1) {
                        monitorKeyList.push(item.monitor_key)
                    }
                });
                // 更新room
                for (let i = 0; i < roomList.length; i++) {
                    await this.ctx.model.RabbitCollect.update({
                        room_text: dictMap[roomList[i]],
                    }, {
                        where: {
                            room: roomList[i],
                        }
                    })
                }
                // 更新room_out
                for (let i = 0; i < roomOutList.length; i++) {
                    await this.ctx.model.RabbitCollect.update({
                        room_out_text: dictMap[roomOutList[i]],
                    }, {
                        where: {
                            room_out: roomOutList[i],
                        }
                    })
                }
                // 更新use
                for (let i = 0; i < useList.length; i++) {
                    await this.ctx.model.RabbitCollect.update({
                        use_text: dictMap[useList[i]],
                    }, {
                        where: {
                            use: useList[i],
                        }
                    })
                }
                // 更新monitor_key
                for (let i = 0; i < monitorKeyList.length; i++) {
                    await this.ctx.model.RabbitCollect.update({
                        monitor_key_text: dictMap[monitorKeyList[i]],
                    }, {
                        where: {
                            monitor_key: monitorKeyList[i],
                        }
                    })
                }
            }
            /**
             * rabbitVideoRecords
             */

            if (key === 'rabbitVideoRecords') {
                const autocollectList = await this.ctx.model.RabbitVideoRecords.findAll({});
                const roomList = []
                const roomOutList = []
                const useList = []
                const monitorKeyList = []
                autocollectList.forEach(item => {
                    //  去重复
                    if (roomList.indexOf(item.room) === -1) {
                        roomList.push(item.room)
                    }
                    if (roomOutList.indexOf(item.room_out) === -1) {
                        roomOutList.push(item.room_out)
                    }
                    if (useList.indexOf(item.use) === -1) {
                        useList.push(item.use)
                    }
                    if (monitorKeyList.indexOf(item.monitor_key) === -1) {
                        monitorKeyList.push(item.monitor_key)
                    }
                });
                // 更新room
                for (let i = 0; i < roomList.length; i++) {
                    await this.ctx.model.RabbitVideoRecords.update({
                        room_text: dictMap[roomList[i]],
                    }, {
                        where: {
                            room: roomList[i],
                        }
                    })
                }
                // 更新room_out
                for (let i = 0; i < roomOutList.length; i++) {
                    await this.ctx.model.RabbitVideoRecords.update({
                        room_out_text: dictMap[roomOutList[i]],
                    }, {
                        where: {
                            room_out: roomOutList[i],
                        }
                    })
                }
                // 更新use
                for (let i = 0; i < useList.length; i++) {
                    await this.ctx.model.RabbitVideoRecords.update({
                        use_text: dictMap[useList[i]],
                    }, {
                        where: {
                            use: useList[i],
                        }
                    })
                }
                // 更新monitor_key
                for (let i = 0; i < monitorKeyList.length; i++) {
                    await this.ctx.model.RabbitVideoRecords.update({
                        monitor_key_text: dictMap[monitorKeyList[i]],
                    }, {
                        where: {
                            monitor_key: monitorKeyList[i],
                        }
                    })
                }

            }

            /**
             * report
             */

            if (key === 'report') {
                const autocollectList = await this.ctx.model.Report.findAll({});
                const roomList = []
                const monitorKeyList = []
                autocollectList.forEach(item => {
                    //  去重复
                    if (roomList.indexOf(item.room) === -1) {
                        roomList.push(item.room)
                    }
                    if (monitorKeyList.indexOf(item.result_key) === -1) {
                        monitorKeyList.push(item.result_key)
                    }
                });
                // 更新room
                for (let i = 0; i < roomList.length; i++) {
                    await this.ctx.model.Report.update({
                        room_text: dictMap[roomList[i]],
                    }, {
                        where: {
                            room: roomList[i],
                        }
                    })
                }
                // 更新monitor_key
                for (let i = 0; i < monitorKeyList.length; i++) {
                    await this.ctx.model.Report.update({
                        result_key_text: dictMap[monitorKeyList[i]],
                    }, {
                        where: {
                            result_key: monitorKeyList[i],
                        }
                    })
                }
            }
        }

        return true

    }
}

module.exports = RouterService;
